package agu.misc;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class MapUtil
{
    public static <K, V> InlineMap<K, V> of(){
        return new InlineMap<K, V>();
    }

    public static <K, V> InlineMap<K, V> of(K key, V value){
        return new InlineMap<K, V>().add(key, value);
    }

    public static <K, V> InlineMap<K, V> of(Map<? extends K, ? extends V> map){
        return new InlineMap<K, V>().addAll(map);
    }

    public static class InlineMap<K, V> extends LinkedHashMap<K, V>
    {
        public InlineMap<K, V> add(K key, V value){
            put(key, value);
            return this;
        }

        public InlineMap<K, V> addAll(Map<? extends K, ? extends V> map){
            putAll(map);
            return this;
        }

        public InlineMap<K, V> del(Object key){
            remove(key);
            return this;
        }

        public InlineMap<K, V> delAll(){
            clear();
            return this;
        }

        public InlineMap<K, V> trim(){
            for(Iterator<Entry<K, V>> it = entrySet().iterator(); it.hasNext(); ){
                Entry<K, V> e = it.next();
                Object key = e.getKey();
                Object value = e.getValue();
                if(key == null || value == null){
                    it.remove();
                }
            }
            return this;
        }

        public Map<K, V> toMap(){
            return this;
        }
    }
}
